package
{
	import Components.MovementComponent;
	
	import flash.display.Sprite;
	import flash.geom.Point;

	public class ResourceGatherer
	{
		public var movementComponent:MovementComponent = new MovementComponent;		
		public var resourceNode:ResourceNode;
		public var resourceAmount:int = 0;
		public var resourceType:int;
		public var homeBase:HomeBase;
		public var display:Sprite = new Sprite();
		
		public function ResourceGatherer()
		{
			var bg:Sprite = VTUtil.getRectOutline(10,10,0x00fddf,3);
			bg.x = -bg.width/2;
			bg.y = -bg.height/2;
			display.addChild(bg);			
		}
		
		public function update(delta:int):void{
			// If this resource gatherer is inside a resource node, early escape
			if(resourceNode.isInside(this)){
				return;
			}
			
			movementComponent.update(delta);
			
			// If no resources in hand, go towards node, otherwise go home
			if(resourceAmount == 0){
				movementComponent.destination = resourceNode.movementComponent.location.clone();
				if(movementComponent.isAtDestination() && resourceNode.canEnter()){
					resourceNode.enter(this);
				}
			} else {
				movementComponent.destination = homeBase.movementComponent.location.clone();
				if(movementComponent.isAtDestination()){
					switch(resourceType){
						case rts1.RESOURCE_TYPE_GAS:
							homeBase.gasGathered += resourceAmount;		
							break;
						case rts1.RESOURCE_TYPE_MINERAL:
							homeBase.mineralGathered += resourceAmount;
							break;
					}
					
					resourceAmount = 0;	
				}
			}
			
			display.x = movementComponent.location.x * rts1.RATIO.x;
			display.y = movementComponent.location.y * rts1.RATIO.y;			
		}
	}
}